Coding Style: PEP 8

You are now starting to write Python programs that have a little substance. Your programs are growing a little longer, and there is a little more structure to your programs. This is a really good time to consider your overall style in writing code.

Why do we need style conventions?

The people who originally developed Python made some of their decisions based on the realization that code is read much more often than it is written. The original developers paid as much attention to making the language easy to read, as well as easy to write.

Python has gained a lot of respect as a programming language because of how readable the code is. You have seen that Python uses indentation to show which lines in a program are grouped together. This makes the structure of your code visible to anyone who reads it.

There are, however, some styling decisions we get to make as programmers that can make our programs more readable for ourselves, and for others.

There are several audiences to consider when you think about how readable your code is.

Yourself, 6 months from now

  • You know what you are thinking when you write code for the first time. But how easily will you recall what you were thinking when you come back to that code tomorrow, next week, or six months from now? We want our code to be as easy to read as possible six months from now, so we can jump back into our projects when we want to.

Other programmers you might want to collaborate with

  • Every significant project is the result of collaboration these days. If you stay in programming, you will work with others in jobs and in open source projects. If you write readable code with good commments, people will be happy to work with you in any setting.

Potential employers

  • Most people who hire programmers will ask to see some code you have written, and they will probably ask you to write some code during your interview. If you are in the habit of writing code that is easy to read, you will do well in these situations.

What is a PEP?

A PEP is a Python Enhancement Proposal.

One of the earliest PEPs was a collection of guidelines for writing code that is easy to read. It was PEP 8, the Style Guide for Python Code.

When people want to suggest changes to the actual Python language, someone drafts a Python Enhancement Proposal.

There is a lot in there that won't make sense to you for some time yet, but there are some suggestions that you should be aware of from the beginning. Starting with good style habits now will help you write clean code from the beginning, which will help you make sense of your code as well.

Basic Python style guidelines

Indentation

  • Use 4 spaces for indentation. This is enough space to give your code some visual structure, while leaving room for multiple indentation levels. There are configuration settings in most editors to automatically convert tabs to 4 spaces, and it is a good idea to check this setting. On Geany, this is under Edit>Preferences>Editor>Indentation; set Width to 4, and Type to Spaces.

Line Length

  • Use up to 79 characters per line of code, and 72 characters for comments. This is a style guideline that some people adhere to and others completely ignore. This used to relate to a limit on the display size of most monitors. Now almost every monitor is capable of showing much more than 80 characters per line. But we often work in terminals, which are not always high-resolution. We also like to have multiple code files open, next to each other. It turns out this is still a useful guideline to follow in most cases. There is a secondary guideline of sticking to 99 characters per line, if you want longer lines.

Many editors have a setting that shows a vertical line that helps you keep your lines to a certain length.

Blank Lines

  • Use single blank lines to break up your code into meaningful blocks. You have seen this in many examples so far. You can use two blank lines in longer programs, but don't get excessive with blank lines.

Comments

  • Use a single space after the pound sign at the beginning of a line. If you are writing more than one paragraph, use an empty line with a pound sign between paragraphs.

Naming Variables

  • Name variables and program files using only lowercase letters, underscores, and numbers. Python won't complain or throw errors if you use capitalization, but you will mislead other programmers if you use capital letters in variables at this point.

That's all for now. We will go over more style guidelines as we introduce more complicated programming structures. If you follow these guidelines for now, you will be well on your way to writing readable code that professionals will respect.

Import statements

PEP8 provides clear guidelines about where import statements should appear in a file. The names of modules should be on separate lines:


In [1]:
# this
import sys
import os

# not this
import sys, os

The names of classes can be on the same line:


In [3]:
from rocket import Rocket, Shuttle

Imports should always be placed at the top of the file. When you are working on a longer program, you might have an idea that requires an import statement. You might write the import statement in the code block you are working on to see if your idea works. If you end up keeping the import, make sure you move the import statement to the top of the file. This lets anyone who works with your program see what modules are required for the program to work.

Your import statements should be in a predictable order:

  • The first imports should be standard Python modules such as sys, os, and math.
  • The second set of imports should be "third-party" libraries. These are libraries that are written and maintained by independent programmers, which are not part of the official Python language.

Module and class names

Modules should have short, lowercase names. If you want to have a space in the module name, use an underscore.

Class names should be written in CamelCase, with an initial capital letter and any new word capitalized. There should be no underscores in your class names.

This convention helps distinguish modules from classes, for example when you are writing import statements.

Exercises

Skim PEP 8

  • If you haven't done so already, skim PEP 8 - Style Guide for Python Code. As you continue to learn Python, go back and look at this every once in a while. I can't stress enough that many good programmers will take you much more seriously from the start if you are following community-wide conventions as you write your code.

Implement PEP 8

  • Take three of your longest programs, and add the extension _pep8.py to the filename of each program. Revise your code so that it meets the styling conventions listed above.

In [ ]:
# Ex 3.28 : Skim PEP 8

# put your code here

In [ ]:
# Ex 3.29 : Implement PEP 8

# put your code here